package com.wsjc.common.core.id.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.wsjc.common.core.config.JedisConfig;
import com.wsjc.common.core.id.IdGenerator;
import com.wsjc.common.core.id.util.IdGeneratorUtil;
import com.wsjc.common.core.utils.DateUtils;


/**
 * 分布式ID生成接口实现类.
 * 
 * @author .
 *
 */
@Component("idGenerator")
public class IdGeneratorImpl implements IdGenerator {
	
	private static final Log logger = LogFactory.getLog(IdGeneratorImpl.class);
	
	private static String ID_GENERATOR_REDIS_STRING_ID_KEY = JedisConfig.readConfig("id.generator.redis.string.id.key") ;
	private static String ID_GENERATOR_REDIS_STRING_ID_VALUE = JedisConfig.readConfig("id.generator.redis.string.id.value") ;

	private static IdGeneratorUtil idGenerator = null;
	

	static {
		try {			
			idGenerator = new IdGeneratorUtil(JedisConfig.readConfig("id.generator.redis.master.ip"), Integer.valueOf(JedisConfig.readConfig("id.generator.redis.master.port")));			
			
			//创建String 结构为生成有序id 做准备
			if(!idGenerator.exists(JedisConfig.readConfig("id.generator.redis.string.user.key"))){
				//用户id
				idGenerator.save(JedisConfig.readConfig("id.generator.redis.string.user.key"), ID_GENERATOR_REDIS_STRING_ID_VALUE, -1) ;
			} 
			if(!idGenerator.exists("id.generator.redis.string.hospital.key")){
				//医院id
				idGenerator.save(JedisConfig.readConfig("id.generator.redis.string.hospital.key"), ID_GENERATOR_REDIS_STRING_ID_VALUE, -1) ;
			}
			if(!idGenerator.exists("id.generator.redis.string.push.key")){
				//push id
				idGenerator.save(JedisConfig.readConfig("id.generator.redis.string.push.key"), ID_GENERATOR_REDIS_STRING_ID_VALUE, -1) ;
			}
			if(!idGenerator.exists("id.generator.redis.string.sterilization.key")){
				//灭菌id
				idGenerator.save(JedisConfig.readConfig("id.generator.redis.string.sterilization.key"), ID_GENERATOR_REDIS_STRING_ID_VALUE, -1) ;
			}

		} catch (Exception e) {
			logger.error("IDGENERATOR-ERROR|IdGeneratorImpl-static()|初始化失败", e);
		}
	}

	
	
	/** #生成有序的id 对应的redis String 的 key **/
	
	@Override
	public String getUsersId(String scope) {
		if(null == scope || "".equals(scope)){
			scope = "JITUAN";
		}
		StringBuffer buf = new StringBuffer();
		buf.append(ID_GENERATOR_REDIS_STRING_ID_KEY).append("U").append(scope);
		buf.append(DateUtils.getReqDate()).append(idGenerator.getIncr(JedisConfig.readConfig("id.generator.redis.string.user.key")));
		
		return buf.toString();
	}


	@Override
	public String getHospitalId(String scope, String hospitalType) {
		
		StringBuffer buf = new StringBuffer();
		buf.append(ID_GENERATOR_REDIS_STRING_ID_KEY).append("H").append(hospitalType).append(scope);
		buf.append(DateUtils.getReqDate()).append(idGenerator.getIncr(JedisConfig.readConfig("id.generator.redis.string.hospital.key")));
		
		return buf.toString();
	}


	@Override
	public String getDataId(String scope, String dataType) {
		
		StringBuffer buf = new StringBuffer();
		buf.append(ID_GENERATOR_REDIS_STRING_ID_KEY).append(dataType).append(scope);
		buf.append(DateUtils.getReqDate()).append(idGenerator.getIncr(JedisConfig.readConfig("id.generator.redis.string.sterilization.key")));
		
		return buf.toString();
	}


	@Override
	public String getPushId(String scope) {
		StringBuffer buf = new StringBuffer();
		buf.append(ID_GENERATOR_REDIS_STRING_ID_KEY).append("P").append(scope);
		buf.append(DateUtils.getReqDate()).append(idGenerator.getIncr(JedisConfig.readConfig("id.generator.redis.string.push.key")));
		
		return buf.toString();
	}

	

}
